package com.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.ChongzhiDAO;
import com.dao.RechargeStationDAO;
import com.pojo.Chongzhi;
import com.pojo.Dictionary;
import com.util.ValidateHelper;
import com.vo.ChongzhiVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("czService")
public class ChongzhiService extends ServiceImpl<ChongzhiDAO, Chongzhi> {

    @Autowired
    private ChongzhiDAO czDao;

    @Autowired
    private DictionaryService dictionaryService;

    @Autowired
    private RechargeStationDAO rechargeStationDAO;

    public IPage<ChongzhiVo> getCzRecordByUser(int page, int limit , String staffId, String time1, String time2){
        IPage<ChongzhiVo> czList = czDao.getCzRecordByUser(new Page(page,limit),staffId,time1, time2);
        return czList;
    }

    public int saveCz(Chongzhi cz){
        return czDao.insert(cz);
    }

    public IPage<ChongzhiVo> getCzRecord(int page, int limit, String staffName, String time1, String time2, String queryStr) {
        List<String> zyNameList = new ArrayList<String>();
        if (ValidateHelper.isNotEmptyString(queryStr)) {
            List<Dictionary> zyList = new ArrayList<Dictionary>();
            if ("gs".equals(queryStr)) {
                zyList = dictionaryService.getDicByZyGs();
            }else if ("jt".equals(queryStr)) {
                zyList = dictionaryService.getDicByZyJt();
            }
            if (ValidateHelper.isNotEmptyCollection(zyList)) {
                for (Dictionary dictionary : zyList) {
                    if (ValidateHelper.isNotEmptyString(dictionary.getName())) {
                        zyNameList.add(dictionary.getName());
                    }
                }
            }
        }
        return czDao.getCzRecord(new Page(page,limit), staffName, time1, time2, queryStr, zyNameList);
    }

    public List<ChongzhiVo> exportByExecl(String staffName, String time1, String time2, String queryStr) {
        List<String> zyNameList = new ArrayList<String>();
        if (ValidateHelper.isNotEmptyString(queryStr)) {
            List<Dictionary> zyList = new ArrayList<Dictionary>();
            if ("gs".equals(queryStr)) {
                zyList = dictionaryService.getDicByZyGs();
            }else if ("jt".equals(queryStr)) {
                zyList = dictionaryService.getDicByZyJt();
            }
            if (ValidateHelper.isNotEmptyCollection(zyList)) {
                for (Dictionary dictionary : zyList) {
                    if (ValidateHelper.isNotEmptyString(dictionary.getName())) {
                        zyNameList.add(dictionary.getName());
                    }
                }
            }
        }
        return czDao.exportByExecl(staffName, time1, time2, queryStr, zyNameList);
    }

    public BigDecimal getTotalAmount(){
        return czDao.getTotalAmount();
    }

    public List<Map<String,Object>> getDayAmountByMonth(){
        return czDao.getDayAmountByMonth();
    }


    /**
     * @author zxj
     *
     * 根据充值点的充值记录
     * @param stationId 充值点ID
     * @param page
     * @param limit
     * @param staffName 会员姓名/手机号/编号/身份证
     * @param startTime 开始日期
     * @param endTime 结束日期
     * @return
     */
    public IPage<ChongzhiVo> getCzRecordByRechargeStation(String stationId, int page, int limit, String staffName, String startTime, String endTime) {
        return czDao.getCzRecordByRechargeStation(new Page(page,limit),stationId,staffName,startTime,endTime);
    }


    /**
     * 获取充值点累计充值金额
     * @return
     */
    public BigDecimal getRechargeTotalAmount () {
        return czDao.getRechargeTotalAmount();
    }


    /**
     * 统计今天充值点充值总金额
     * @return
     */
    public BigDecimal getRechargeDayAmount () {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String curDate = sdf.format(new Date());
        return czDao.getRechargeDayAmount(curDate);
    }

    /**
     * 统计当前月份充值点充值总金额
     * @return
     */
    public BigDecimal getRechargeMonthAmount () {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String curDate = sdf.format(new Date());
        return czDao.getRechargeMonthAmount(curDate);
    }


    /**
     * 统计每个充值点的充值金额
     * @return
     */
    public List<Map<String,Object>> getRechargeAmount () {
        return czDao.getRechargeAmount();
    }

    /**
     * 获取最近12个月的月份
     * @return
     */
    public IPage getCurYearRechargeAmount(int page, int limit) {
        Page p = new Page(page, limit);
        return czDao.getCurYearRechargeAmount(p);
    }



    /**
     * 统计充值点的充值金额，包含昨天，今天，累计三种类型
     * @param stationId
     * @return
     */
    public Map countMoney(@Param("stationId") String stationId, String monthStart, String monthEnd){
        return czDao.countMoney(stationId,monthStart,monthEnd);
    }

    /**
     * 统计近一个月每天的充值金额
     * @param stationId
     * @return
     */
    public List<Map<String,Object>> getDayAmountOfMonthByStation(String stationId){
        return czDao.getDayAmountOfMonthByStation(stationId);
    }

    /**
     * 统计昨天各充值点的充值金额
     * @return
     */
    public List<Map<String,Object>> countAmountByDay(){
        return czDao.countAmountByDay();
    }
}
